<?php
/**

* @property CI_Loader $load

* @property CI_Form_validation $form_validation

* @property CI_Input $input

* @property CI_Email $email

* @property CI_DB_active_record $db

* @property CI_DB_forge $dbforge
  
* @property Users $users

*/
class Login extends CI_Controller {

    //Constructor for Login
    function Login() {
        parent::__construct();
        $this->load->model('users');
        $this->load->library('session');
        $this->load->helper('url');
    }

    /* this method checks whether user is already logged in or not
     * if user is already logged in redirected to Problem Statements page,
     * else login page will be displayed
     */

    function index() {
        $error['login_error'] = $this->session->flashdata('login_error');

        //$userid=$this->session->userdata('userid');
        if (isset($userid) && $userid != null) {
            echo 'login successfull';
        } else {
            $this->load->view('login', $error);
        }
    }

    /*
     * Check for username and password and redirect accordingly on Login.
     */

    function go() {
        if ($this->input->post('username') != null) {
            $username = $this->input->post('username');
            $password = $this->input->post('password');

            $login_result = $this->users->login($username, $password);

            if ($login_result == FALSE) {
                $this->session->set_flashdata('login_error', 'Username or password is wrong.');
                redirect('login');
            } else {
                $this->session->set_userdata('userid', $login_result);
                redirect('problemcategorylist');
                //echo 'Login Successfull';
            }
        } else {
            redirect('login');
        }
    }

    /*
     * Register new user, if username or email already present throw error
     */

    function register() {
        $this->load->view('register');
    }

    /*
     * get the user details if everything is valid then new user will be registered.
     */

    function goregister() {

        if ($this->input->post('username') != null) {
            $username = $this->input->post('username');
            $email = $this->input->post('email');
            $check = $this->db->query('select username,email from users where username="' . $username . '" or email="' . $email . '"');
            //Checking whether username or password alreday registered.
            if ($check->num_rows() > 0) {
                echo 'Email address or username already registered.';
            } else {

                $data = $this->input->post();

                $userdata = array(
                    'id' => 0,
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'college' => $data['college'],
                    'occupation' => $data['occupation'],
                    'dateofbirth' => $data['dateofbirth'],
                    'whyprojectcode' => $data['whyprojectcode'],
                    'aboutyou' => $data['aboutyou'],
                    'username' => $data['username'],
                    'password' => md5($data['password'] . 'secret')
                );

                $this->users->register($userdata);
                $this->sendmail($data);
                echo 'registered successfully!!';
            }
        }
    }

    /*
     * Send mail to user after successful registration
     */

    function sendmail($data) {
        $this->load->library('email');

        $this->email->from('support@chatbot.in', 'ProjectCode Team');
        $this->email->to($data['email']);
        
        $this->email->subject('Successful Registration on ProjectCode');
        $this->email->message('Hello '.$data['name'].',\r\n\tWelcome to ProjectCode! Your Basic Registration is Complete.');

        $this->email->send();

        //echo $this->email->print_debugger();
    }

}

?>
